package br.com.vti.sisp_sicrim.modelo.business;

import javax.validation.ValidationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import br.com.crud4j.core.model.business.BusinessImpl;
import br.com.crud4j.core.model.dao.DAO;
import br.com.vti.sisp_sicrim.model.entity.Usuario;
import br.com.vti.sisp_sicrim.modelo.business.exception.UsuarioException;

/**
 *
 * @author ednardomatos
 */
@Component("usuarioBusiness")
@Transactional("sispSicrimTransactionManager")
public class UsuarioBusinessImpl extends BusinessImpl<Usuario, UsuarioException> {

	@Autowired
	public UsuarioBusinessImpl(@Qualifier("sispSicrimDao") DAO<Usuario> dao) {
		super(dao);
	}

	@Override
	protected void validate(Integer operacao, Usuario entidade) throws ValidationException {
		
		if(operacao == BusinessImpl.INSERIR ||
		   operacao == BusinessImpl.ATUALIZAR ||
		   operacao == BusinessImpl.REMOVER){
			if(entidade != null && entidade.getId() != null && entidade.getId().intValue() == 1){
				throw new ValidationException(getMessageResolver().getMessage("erro.usuario.administrador"));
			}
			
		}
		
		super.validate(operacao, entidade);
	}

	@Override
	public void salvar(Usuario arg0) throws UsuarioException {
		// TODO Auto-generated method stub
		super.salvar(arg0);
	}
	
}
